package com.yljk.yljkproject.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import com.yilijk.base.AppBase;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class RefreshUtils {

    private String saveFilePath;
    private String saveFileName;
    private View inflate;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){

            }
        }
    };


    public void downloadApl(Context context, String apkurl){
        Toast.makeText(context, "开始下载"+apkurl, Toast.LENGTH_SHORT).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(inflate);
        builder.create();
        builder.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(apkurl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    final int contentLength = connection.getContentLength();//文件大小
                    InputStream inputStream = connection.getInputStream();
                    File file = new File(saveFilePath);
                    if(!file.exists()){
                        file.mkdir();
                    }
                    String apkFile = saveFileName;
                    File apkfile = new File(apkFile);
                    FileOutputStream fileOutputStream = new FileOutputStream(apkFile);
                    int count = 0;
                    int lenth = 0;
                    byte[] bytes = new byte[512];
                    while( (lenth = inputStream.read(bytes))!=-1){
                        count+=lenth;
                        fileOutputStream.write(bytes,0,lenth);
                        Message message = new Message();
                        message.arg1 = count;
                        message.arg2 = contentLength;
                        message.what = 2;
                        handler.sendMessage(message);
                        fileOutputStream.flush();
                    }
                    handler.sendEmptyMessage(1);
                    fileOutputStream.close();
                    inputStream.close();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //安装APK
    public void installApl(){
        File file = new File(saveFilePath,"apkName.apk");
        if(!file.exists()){
            return;
        }
        Intent intent=new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
            String string = "com.example.project_class6_sc"+".fileprovider";
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = FileProvider.getUriForFile(AppBase.getApplication(), string, file);
            intent.setDataAndType(uri,"application/vnd.android.package-archive");
        }else{
            intent.setDataAndType(Uri.parse("file://"+file.toString()),"application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        AppBase.getApplication().startActivity(intent);
    }

}
